#!/usr/bin/env dash
# mpd, or mpv.
# planned: left click is toggle pause, right click is next, scroll is mpd volume.

maxWinNameLen=30

player.sh | grep -q playing && playing=true || playing=false
player.sh | grep -q paused && paused=true || paused=false

if $playing || $paused; then
    title="$(player.sh -f "%title% - %artist%" | head -n 1)"
    # if title is empty, try file and trim from last / if exists.
    if [ "$title" = " - " ]; then
        title="$(player.sh -f "%file%" | head -n 1 | sed 's/.*\///')"

        # peel some stuff
        title="$(echo "$title" | sed 's/【.*】//')"
    fi
else
  title="Stopped"
fi

if [  "$title" = "Stopped" ]; then
    icon music
    echo Stopped
else
    icon music
    $playing && icon pause
    $paused && icon play
    echo "$title" | cut -c 1-$maxWinNameLen
fi
